package 打牢基础30天.day04.my;

import java.util.HashSet;

/**
 * @Author yaya
 * @Create 2024/6/14 20:29
 * Description:找到第一个相交的节点
 */
public class Code07_FindFirstIntersectNode {
    public static class Node {
        public int value;
        public Node next;

        Node(int data) {
            this.value = data;
        }
    }

    /**
     * 给两个链表，判断他们是否有交点，如果有返回交点
     *
     * @param head1
     * @param head2
     * @return
     */
    public static Node getIntersectNode(Node head1, Node head2) {
        if (head1 == null || head2 == null) {
            return null;
        }
        // 如果不是环形链表
        // 使用查找两链表交点方法，查询两链表交点
        Node loopNode1 = getLoopNode(head1);
        Node loopNode2 = getLoopNode(head2);

        if (loopNode1 == null && loopNode2 == null) {
            return noLoop(head1, head2);
        }
        // 如果是环形链表
        if (loopNode1 != null && loopNode2 != null) {
            return bothLoop(head1, head2, loopNode1, loopNode2);
        }

        return null;
    }

    private static Node bothLoop(Node head1, Node head2, Node loopNode1, Node loopNode2) {
        // 1、在环外相交
        if (loopNode1 == loopNode2) {
            int n = 0;
            Node temp1 = head1;
            Node temp2 = head2;
            // 计算长度差
            while (temp1 != loopNode1) {
                n++;
                temp1 = temp1.next;
            }
            while (temp2 != loopNode2) {
                n--;
                temp2 = temp2.next;
            }
            // 设置长短链表
            Node longNode = n > 0 ? head1 : head2;
            Node shortNode = longNode == head1 ? head2 : head1;
            n = Math.abs(n);
            // 长的先走n
            while (n != 0) {
                longNode = longNode.next;
                n--;
            }
            while (longNode != shortNode) {
                longNode = longNode.next;
                shortNode = shortNode.next;
            }
            return longNode;
        } else {
            // 2、在环内相交
            Node temp = loopNode1.next;
            // 环内相交，任意节点都可以称之为相交节点
            while (temp != loopNode1) {
                if (loopNode2 == temp) {
                    return loopNode1;
                }
                temp = temp.next;
            }
            // 3、各自为环
            return null;
        }

    }


    /**
     * 查询不循环的两个链表相交
     * 相交：长度差为n，长链表先走n步，后 跟短链表一起走，有相同则相交
     *
     * @param head1
     * @param head2
     * @return
     */
    private static Node noLoop(Node head1, Node head2) {
        if (head1 == null || head2 == null) {
            return null;
        }
        int n = 0;
        Node temp1 = head1;
        Node temp2 = head2;
        // 计算长度差
        while (temp1.next != null) {
            n++;
            temp1 = temp1.next;
        }
        while (temp2.next != null) {
            n--;
            temp2 = temp2.next;
        }

        if (temp1 != temp2) {
            return null;
        }

        // 设置长短链表
        Node longNode = n > 0 ? head1 : head2;
        Node shortNode = longNode == head1 ? head2 : head1;

        n = Math.abs(n);
        // 长的先走n
        while (n != 0) {
            longNode = longNode.next;
            n--;
        }

        while (longNode != shortNode) {
            longNode = longNode.next;
            shortNode = shortNode.next;
        }
        return longNode;
    }


    /**
     * 辅助方法：找到循环链表的第一个进入循环节点的
     * 不使用set
     *
     * @param head
     */
    private static Node getLoopNode(Node head) {
        if (head == null) {
            return null;
        }

        // 使用快慢指针，快指针一次走两个，慢指针一次走一个
        Node low = head.next;
        Node fast = head.next.next;

        while (low != fast) {
            if (fast.next == null || fast == null) {
                return null;
            }
            low = low.next;
            fast = fast.next.next;
        }
        // 如果fast 到null 说明没有循环
        if (fast == null) {
            return null;
        }
        // 如果 low == fast ，在循环中相遇，将fast 置为 head，从新开始一格一格走
        fast = head;
        while (low != fast) {
            fast = fast.next;
            low = low.next;
        }
        // 他们再次相遇的地方就是循环第一个节点
        return fast;
    }

    /**
     * 辅助方法：找到循环链表的第一个进入循环节点的
     * 使用set存储元素，当第二次碰到节点时，说明该节点为进入循环的第一个节点
     *
     * @param head
     */
    private static Node getLoopNode2(Node head) {
        if (head == null) {
            return null;
        }
        HashSet<Node> set = new HashSet<>();
        Node temp = head;
        while (temp != null) {
            if (set.contains(temp)) {
                break;
            }
            set.add(temp);
        }
        return temp == null ? null : temp;
    }

    public static void main(String[] args) {
        // 1->2->3->4->5->6->7->null
        Node head1 = new Node(1);
        head1.next = new Node(2);
        head1.next.next = new Node(3);
        head1.next.next.next = new Node(4);
        head1.next.next.next.next = new Node(5);
        head1.next.next.next.next.next = new Node(6);
        head1.next.next.next.next.next.next = new Node(7);

        // 0->9->8->6->7->null
        Node head2 = new Node(0);
        head2.next = new Node(9);
        head2.next.next = new Node(8);
        head2.next.next.next = head1.next.next.next.next.next; // 8->6
        System.out.println(getIntersectNode(head1, head2).value);

        // 1->2->3->4->5->6->7->4...
        head1 = new Node(1);
        head1.next = new Node(2);
        head1.next.next = new Node(3);
        head1.next.next.next = new Node(4);
        head1.next.next.next.next = new Node(5);
        head1.next.next.next.next.next = new Node(6);
        head1.next.next.next.next.next.next = new Node(7);
        head1.next.next.next.next.next.next = head1.next.next.next; // 7->4

        // 0->9->8->2...
        head2 = new Node(0);
        head2.next = new Node(9);
        head2.next.next = new Node(8);
        head2.next.next.next = head1.next; // 8->2
        System.out.println(getIntersectNode(head1, head2).value);

        // 0->9->8->6->4->5->6..
        head2 = new Node(0);
        head2.next = new Node(9);
        head2.next.next = new Node(8);
        head2.next.next.next = head1.next.next.next.next.next; // 8->6
        System.out.println(getIntersectNode(head1, head2).value);

    }
}
